﻿using System;
using System.Collections.Generic;
using System.Text;

using BEGDC.MIS.Office;

namespace BEGDC.MIS.Office.Excel
{
    public abstract class ExcelService : OfficeService    
    {
        public virtual string TemplateFile
        {
            get
            {
                return Configuration.TemplatePath + "\\" + ServiceName + "\\Definition.xls";
            }
        }

        public virtual void Validate(XmlTable xml_table, System.Data.DataTable dt_table)
        {
            if (xml_table.Rows.Count > 0)
            {
                XmlRow xml_row = xml_table.Rows[0];
                for (int k = 0; k < xml_row.Elements.Count; k++)
                {
                    Element element = xml_row.Elements[k];

                    if (element.Validation != null && element.Validation != string.Empty)
                    {
                        IValidation validation = (IValidation)System.Reflection.Assembly.Load("BEGDC.MIS.Office").CreateInstance("BEGDC.MIS.Office.Excel." + element.Validation);

                        int iIndex = 2;
                        for(int i = 0 ; i < dt_table.Rows.Count ; i++)
                        //foreach(System.Data.DataRow dr in dt_table.Rows)
                        {

                            bool isEmptyRow = true;

                            System.Data.DataRow dr = dt_table.Rows[i];

                            foreach (object strValue in dr.ItemArray)
                            {
                                
                                if (strValue != System.DBNull.Value && ((string)strValue) != string.Empty)
                                {
                                    isEmptyRow = false;
                                    break;
                                }
                            }

                            if (isEmptyRow)
                            {
                                dt_table.Rows.RemoveAt(i);
                                i--;
                                continue;
                            }

                            element.Position = element.Position.Substring(0,1) + iIndex.ToString();
                            validation.Validate(element, dr[element.Name].ToString());
                            iIndex++;
                        }
                    }
                }
            }
        }
    }
}
